<?php

/**
 * 发送验证码
 * @param $phone
 * @param $content
 * @return bool
 */
function lt_send_sms($phone,$content){
    header("Content-type:text/html;charset=utf-8");
    vendor("sms.sms");
    $model = new \Sms('200894','710294');
    $res = $model->SendSms($phone,$content);
    if(!empty($res['response_body'])){
        return true;
    }else{
        return false;
    }
}

/**
 * post请求
 * @param $url
 * @param $data
 * @return mixed
 */
function lt_curl_post($url,$data){
    $ch = curl_init();
    $this_header = ['Content-Type: application/x-www-form-urlencoded; charset=utf-8;'];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
/**
 * 根据token获取用户
 * @param $token
 * @return bool|mixed
 */
function lt_get_user_by_token($token){
    $user=M('User')->where(['token'=>$token])->find();
    if(!$user){
        return false;
    }else{
        if($user['token_expire_time']<time()){
            return false;
        }else{
            return $user;
        }
    }
}

/**
 * 根据token获取用户
 * @param $token
 * @return bool|mixed
 */
function lt_get_business_by_token($token){
    $business=M('Business')->where(['token'=>$token])->find();
    if(!$business){
        return false;
    }else{
        if($business['token_expire_time']<time()){
            return false;
        }else{
            return $business;
        }
    }
}

/**
 * 最准确获取文件后缀
 * @param $file
 * @return mixed
 */
function lt_get_extension($file)
{
    return pathinfo($file, PATHINFO_EXTENSION);
}

/**
 * 判断字符串是否为空
 * @param $str
 * @return bool
 */
function lt_str_is_empty($str){
    $str=trim($str);
    return empty($str);
}

/**
 * 验证是否是手机号码
 * @param $telephone
 * @return bool
 */
function lt_is_telephone($telephone)
{
    if (preg_match("/^1[34578]\d{9}$/", $telephone)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 全局过滤函数
 * @param $html
 * @return string
 */
function lt_htmlspecialchars($html)
{
    return htmlspecialchars(trim($html), ENT_QUOTES);
}

/**
 * @return string 生成唯一token
 */
function lt_create_token(){
    return uniqid();
}

/**
 * 根据id获取商户
 * @param $id
 * @return mixed
 */
function lt_get_business_by_id($id){
    $id=intval($id);
    $business=M('Business')->where(['id'=>$id])->find();
    if(!$business){
        return false;
    }else{
        return $business;
    }
}

/**
 * 根据id获取商户
 * @param $id
 * @return mixed
 */
function lt_get_normal_business_by_id($id){
    $id=intval($id);
    $business=M('Business')->where(['id'=>$id,'status'=>1,'is_checked'=>1])->find();
    if(!$business){
        return false;
    }else{
        return $business;
    }
}

/**
 * 插入信息表
 * @param $title string 标题
 * @param $content string 内容
 * @param $user_id int 用户id
 * @return bool
 */
function lt_add_message($title,$content,$user_id){
    //发送推送消息
    $user=M('User')->where(['id'=>$user_id])->field('token')->find();
    lt_jpush($user['token']);
    $data['title']=$title;
    $data['content']=$content;
    $data['user_id']=intval($user_id);
    $data['create_time']=time();
    $result=M('Message')->add($data);
    if ($result===false) {
        return false;
    }else{
        return true;
    }
}

/**
 * 极光推送用户端
 * @param $token
 * @param string $message
 * @param string $name
 */
function lt_jpush($token,$message='您有新消息了',$name='1.mp3'){
    vendor("jpush.autoload");
    $client=new JPush\Client('9d8e29d2c64b65af11043e87','9f35ea76e00872c8a29d3548');
    $pusher = $client->push();
    $pusher->setPlatform('all');
    $pusher->addAlias(["$token"]);
    $pusher->iosNotification($message, array(
        'sound' => $name,
    ));
    $pusher->androidNotification($message, array(
        'title' => $message,
        // 'build_id' => 2,
        'extras' => array(
            'type'=>'sound',
            'filename' => $name
        ),
    ));
    $pusher->setOptions(null,null,null,true);
    try {
        $pusher->send();
    } catch (\JPush\Exceptions\JPushException $e) {
        // try something else here
       // print $e;
    }
}

/**
 * 极光推送商户端
 * @param $token
 * @param string $message
 * @param string $name string 音频文件名
 */
function lt_business_jpush($token,$message='您有新订单了',$name='2.mp3'){
    vendor("jpush.autoload");
    $client=new JPush\Client('98d91c3d9e1f026210c0ae94','c00ac6b233448636dd5ce229');
    $pusher = $client->push();
    $pusher->setPlatform('all');
    $pusher->addAlias(["$token"]);
    $pusher->iosNotification($message, array(
        'sound' => $name,
    ));
    $pusher->androidNotification($message, array(
        'title' => $message,
        // 'build_id' => 2,
        'extras' => array(
            'type'=>'sound',
            'filename' => $name
        ),
    ));
    $pusher->setOptions(null,null,null,true);
    try {
        $pusher->send();
    } catch (\JPush\Exceptions\JPushException $e) {
        // try something else here
      //   print $e;
    }
}

/**
 * 推送商户消息
 * @param $id
 */
function lt_check_and_push($id){
    $business=M('Business')->where(['id'=>intval($id)])->field('token,token_expire_time')->find();
    if($business&&$business['token']&&$business['token_expire_time']>time()){
        lt_business_jpush($business['token']);
    }
}
/**
 * @desc 根据两点间的经纬度计算距离
 * @param float $lat1 纬度值
 * @param float $lng1 纬度值
 * @param float $lat2 纬度值
 * @param float $lng2 经度值
 * @return string 距离
 */
function lt_get_distance($lat1, $lng1, $lat2, $lng2){
    $earthRadius = 6367000; //approximate radius of earth in meters
    $lat1 = ($lat1 * pi() ) / 180;
    $lng1 = ($lng1 * pi() ) / 180;
    $lat2 = ($lat2 * pi() ) / 180;
    $lng2 = ($lng2 * pi() ) / 180;
    $calcLongitude = $lng2 - $lng1;
    $calcLatitude = $lat2 - $lat1;
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;
    return round($calculatedDistance);
}
/**
 * 2维数组根据字段排序
 * @param $arrays
 * @param $sort_key
 * @param int $sort_order
 * @param int $sort_type
 * @return array|bool
 */
function lt_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
    if(is_array($arrays)){
        foreach ($arrays as $array){
            if(is_array($array)){
                $key_arrays[] = $array[$sort_key];
            }else{
                return false;
            }
        }
    }else{
        return false;
    }
    array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
    return $arrays;
}

/**
 * 获取正常商户id字段
 * @param $id
 * @return bool|mixed
 */
function lt_get_simple_business_by_id($id){
    $id=intval($id);
    $business=M('Business')->where(['id'=>$id,'status'=>1,'is_checked'=>1])->field('id')->find();
    if(!$business){
        return false;
    }else{
        return $business;
    }
}

/**
 * 获取id=>name键值对的数组
 * @return array|mixed
 */
function lt_get_scopes_group(){
    $scopes=M('Scope')->field('id,name')->select();
    $scope_names=array_column($scopes,'name');
    $scope_ids=array_column($scopes,'id');
    $scopes=array_combine($scope_ids,$scope_names);
    return $scopes;
}

/**
 * 根据id生成二维码
 * @param $content
 * @param $filename mixed 生成文件名
 * @return string 存入库的路径
 */
function lt_create_png($content,$filename=false){
    $content=sp_authencode($content);
    vendor('Qrcode.phpqrcode');
    \QRcode::png($content,$filename);
    return str_replace('./data/upload/','',$filename);
}

/**
 * 获取二维码存放路径
 * @param $name
 * @return string
 */
function lt_get_erweima_file($name){
    return './data/upload/erweima/'.sp_authencode($name).'.png';
}

/**
 * 获取上周一
 * @param $time int 时间戳
 * @return int
 */
function lt_get_last_monday($time){
    $day=date("w",$time);
    if($day!=1){
        $time=strtotime("Last Monday",$time);
    }else{
        $time=strtotime(date("Y-m-d",$time));
    }
    return $time;
}

/**
 * 获取上周日
 * @param $time
 * @return int
 */
function lt_get_next_sunday($time){
    $day=date("w",$time);
    if($day!=0){
        $time=strtotime("Next Sunday",$time);
    }else{
        $time=strtotime(date("Y-m-d",$time));
    }
    return $time;
}

/**
 * 获取订单信息
 * @param $sn string 订单sn
 * @param null $uid int 用户id
 * @return bool|mixed
 */
function lt_get_order_by_sn($sn,$uid=null){
    $where['sn']=$sn;
    if($uid){
        $where['user_id']=intval($uid);
    }
    $order=M("Order")->where($where)->find();
    if(!$order){
        return false;
    }
    return $order;
}

/**
 * 获取未支付订单
 * @param $sn
 * @param null $uid
 * @return bool|mixed
 */
function lt_get_unpaid_order_by_sn($sn, $uid = null)
{
    $order = lt_get_order_by_sn($sn, $uid);
    if (!$order) {
        return false;
    }
    //非等待支付
    if ($order['status'] != 0) {
        return false;
    }
    return $order;
}

/**
 * 获取订单信息
 * @param $sn string 订单sn
 * @param null $uid int 用户id
 * @return bool|mixed
 */
function lt_get_activity_order_by_sn($sn,$uid=null){
    $where['sn']=$sn;
    if($uid){
        $where['user_id']=intval($uid);
    }
    $order=M("OrderActivity")->where($where)->find();
    if(!$order){
        return false;
    }
    return $order;
}

/**
 * 获取未支付订单
 * @param $sn
 * @param null $uid
 * @return bool|mixed
 */
function lt_get_unpaid_activity_order_by_sn($sn, $uid = null)
{
    $order = lt_get_activity_order_by_sn($sn, $uid);
    if (!$order) {
        return false;
    }
    //非等待支付
    if ($order['status'] != 0) {
        return false;
    }
    return $order;
}

/**
 * 通过商户id获取最近的活动
 * @param $id
 * @return mixed
 */
function lt_get_nearly_simple_activity_by_business($id){
    $businessActivityNearly=M('BusinessActivity')->where(['business_id'=>intval($id),'end_time'=>['EGT',time()],'is_deleted'=>0])->order('end_time')->field('id')->find();
    return $businessActivityNearly;
}

/**
 * 根据服务id获取全部服务名
 * @param $id
 * @return string
 */
function lt_get_service_full_name($id){
    $service=M('Service')->where(['id'=>intval($id)])->find();
    if($service['pid']!=0){
        $service_parent=M('Service')->where(['id'=>$service['pid']])->find();
        $service_name=$service_parent['name'].'-'.$service['name'];
    }else{
        $service_name=$service['name'];
    }
    return $service_name;
}

/**
 * 获得省份
 */
function lt_get_provinces()
{
    $region = M('Region');
    $provinces = $region->where(['pid' => 0])->select();
    return $provinces;
}

/**
 * 根据省份id获取下面的市
 * @param $id
 * @return mixed
 */
function lt_get_city_by_province_id($id)
{
    $region = M('Region');
    $cities = $region->where(['pid' => intval($id)])->select();
    if ($cities) {
        return $cities;
    }else{
        return false;
    }
}

/**
 * 根据城市id获取下面的区
 * @param $id
 * @return mixed
 */
function lt_get_district_by_city_id($id)
{
    $region = M('Region');
    $districts = $region->where(['pid' => intval($id)])->select();
    if ($districts) {
        return $districts;
    }else{
        return false;
    }
}

/**
 * 将xml转换为数组
 * @param $xml
 * @return mixed
 */
function simplest_xml_to_array($xml) {
    return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}